import roleApi from '@/biz/role'
import dt from '@dt/dt'

function getSysRoleList(roleName, params) {
  let roles = []
  let data = {
    roleName,
    ...params
  }
  return roleApi.getSysRoleList(data).then((res) => {
    res.forEach((item) => {
      item.key = item.id.toString()
      item.value = item.roleName
      roles.push(item)
    })
    return roles
  })
}

function createSysRole(opt = {}, params) {
  return {
    title: opt.title || '角色',
    placeholder: opt.placeholder || '请选择角色',
    key: opt.key,
    disabled: opt.disabled,
    col: opt.col || 24,
    load: (o) => {
      getSysRoleList('', params).then((res) => {
        o.select.options = res
      })
    },
    select: {
      options: [],
      multiple: opt.multiple,
      maxTagCount: opt.maxTagCount,
      filterable: true,
      remote: (key) => {
        return getSysRoleList(key, params)
      }
    },
    data: opt.data,
    rules: opt.rules || [
      {
        required: true,
        type: opt.type,
        message: '请选择角色',
        trigger: 'blur'
      }
    ]
  }
}
function create() {
  return {}
}
export default {
  create,
  createSysRole
}
